package com.aaa.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author ：zhouhangyong
 * @date ：Created in 2022/6/15 8:36
 * @description：实现接口
 * @modified By：
 * @version:
 */
public class ProxyCGLib {
    public Object  getObjectCG(Class<?  extends Object> trangeClass){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(trangeClass);
        enhancer.setCallback(new MethodInterceptor() {
            /**
             * @Author zhouhangyong
             * @Description //TODO
             * @Date 9:03 2022/6/15
             * @Param [o, method, objects, methodProxy]
             * @return java.lang.Object
             **/
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                Object invokeSuper = methodProxy.invokeSuper(o, objects);
                System.out.println(method.getName()+"动态日志已加载" + new Date());
                return invokeSuper;
            }
        });
        return enhancer.create();
    }
}
